package com.oocl.messageproduce;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class MsgProducer {
   
	  private ConnectionFactory factory = null;//
	  private Connection con = null;//
	  private Destination queque =  null;//
	  private MessageProducer producer = null;
	  private Session ses = null;
	  public MsgProducer(String queueName){
		  factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		  init(queueName);
	  }
	  
	  public void init(String queueName){
		  try {
		     queque = new ActiveMQQueue(queueName);
			 con = factory.createConnection();
		     con.start();
		     ses = con.createSession(false, Session.AUTO_ACKNOWLEDGE); //false:关闭事物   true:开启事务  事务完成Session后要进行commit
		     producer = ses.createProducer(queque);
		  } catch (JMSException e) {
			  // TODO Auto-generated catch block
			  e.printStackTrace();
		  }
	  }
	  
	  public void send(String msgStr){
		  try {
			TextMessage msg = ses.createTextMessage(msgStr) ;
			producer.send(msg);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
	  
	  
	  public void close(){
		  try {
			producer.close();
			con.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
}
